import axios from '@/common/api'
import router from '@/router'
import voice from '@/assets/voice/default.mp3'
let utils = {}
utils.setCookie = function (name, value) {
  document.cookie = name + '=' + escape(value)
}

// 设置带时间cookie
utils.setCookieDate = function (name, value) {
  var exp = new Date()
  exp.setTime(exp.getTime() + 60 * 1000 * 120) // 过期时间 30分钟
  document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString() + ';path=/'
}
// 获取cookie
utils.getCookie = function (name) {
  // eslint-disable-next-line one-var
  var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  if ((arr = document.cookie.match(reg))) {
    if (
      unescape(arr[2]).substring(0, 1) === '"' ||
      unescape(arr[2]).substring(0, 1) === '\''
    ) {
      return unescape(arr[2]).substring(1, unescape(arr[2]).length - 1)
    } else {
      return unescape(arr[2])
    }
  } else return null
}
// 清空cookie
utils.delCookie = function (name) {
  var exp = new Date()
  exp.setTime(-1000)
  var cval = utils.getCookie(name)
  if (cval != null) document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
}
export const CurentTime = () => {
  var now = new Date()
  var year = now.getFullYear() // 年
  var month = now.getMonth() + 1 // 月
  var day = now.getDate() // 日
  var hh = now.getHours() // 时
  var mm = now.getMinutes() // 分
  var ss = now.getSeconds() // 秒
  var clock = year + '-'
  if (month < 10) { clock += '0' }
  clock += month + '-'
  if (day < 10) { clock += '0' }
  clock += day + ' '
  if (hh < 10) { clock += '0' }
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return (clock)
}
utils.voicePrompt = function (text) {
  var url = voice
  if (text !== null && text !== '' && text !== 'undefined' && text !== undefined) {
    url = 'http://tts.baidu.com/text2audio?cuid=baiduid&lan=zh&ctp=1&pdt=311&tex=' + text
  } else {
    url = voice
  }
  new Audio(url).play()
}
export const logout = () => {
  return axios.request({
    url: '/member/logout',
    method: 'GET'
  })
    .then(function (res) {
      var status = res.data.code
      if (status === 200) {
        utils.delCookie('JSCOOKIE')
        utils.delCookie('JSCOOKIE_test')
        router.push('/login')
      }
    })
    .catch(function () {

    })
}
var platformName = window.location.host
var protocol = process.env.NODE_ENV === 'development' ? 'https:' : window.location.protocol
var domainName = process.env.NODE_ENV === 'development' ? 'chat.hll666.xyz' : platformName
var basewSocketUrl = protocol === 'https:' ? 'wss://' + domainName + '/websocket/' : 'ws://' + domainName + '/websocket/'
utils.domainName = domainName
utils.basewSocketUrl = basewSocketUrl
utils.successMsgTip = function (name) {
  var locale = localStorage.getItem('locale')
  var successMsgTip = {
    operation: '操作成功',
    RGisInside: '内部会员充值成功！',
    upgrade: '内部会员升级成功！',
    translate: '购买翻译次数',
    RG: '充值成功！',
    islike: '您已喜欢该好友',
    setuser: '用户信息修改成功！',
    noData: '没有更多数据！',
    giftMsg: '你收到了一份好友的礼物请查看',
    friendMsg: '你有新的消息请查收',
    msgEmpty: '动态消息不能为空',
    codeError: '请填写验证码',
    newPwdError: '请填写修改后的密码',
    buyerr: '表情包已经购买过了！',
    buysucess: '购买表情包成功！',
    buyNoCheck: '请选择购买的表情包！',
    buyNoCheckF: '请选择赠送表情包的好友'
  }
  if (locale === 'en-US') {
    successMsgTip.operation = 'Successful operation'
    successMsgTip.RGisInside = 'Successful recharge of internal members'
    successMsgTip.upgrade = 'Internal member upgrade successfully!'
    successMsgTip.translate = 'Purchase translations'
    successMsgTip.RG = 'Recharge successfully'
    successMsgTip.islike = 'You already like this friend'
    successMsgTip.setuser = 'User information modified successfully！'
    successMsgTip.noData = 'No more data!'
    successMsgTip.giftMsg = 'You received a gift from a friend'
    successMsgTip.friendMsg = 'Please check for new news'
    successMsgTip.msgEmpty = 'Dynamic message cannot be empty'
    successMsgTip.codeError = 'Please fill in the verification code'
    successMsgTip.newPwdError = 'Please fill in the new password'
    successMsgTip.buyerr = 'Emoticons have been purchased!'
    successMsgTip.buysucess = 'Successful purchase of emoticons!'
    successMsgTip.buyNoCheck = 'Please choose an emoticon to buy!'
    successMsgTip.buyNoCheckF = 'Please choose a friend who gives an emoticon'
  } else if (locale === 'zh-TW') {
    successMsgTip.operation = '操作成功'
    successMsgTip.RGisInside = '內部會員充值成功！'
    successMsgTip.upgrade = '内部会员升級成功！'
    successMsgTip.translate = '購買翻譯次數'
    successMsgTip.RG = '充值成功！'
    successMsgTip.islike = '您已喜歡該好友'
    successMsgTip.setuser = '用戶信息修改成功！'
    successMsgTip.noData = '沒有更多數據!'
    successMsgTip.giftMsg = '友達から贈り物を受け取りました'
    successMsgTip.friendMsg = '你有新的消息請查收'
    successMsgTip.msgEmpty = '動態消息不能為空'
    successMsgTip.codeError = '請填寫驗證碼'
    successMsgTip.newPwdError = '請填寫修改後的密碼'
    successMsgTip.buyerr = '表情包已經購買過了！'
    successMsgTip.buysucess = '購買表情包成功！'
    successMsgTip.buyNoCheck = '請選擇購買的表情包！'
    successMsgTip.buyNoCheckF = '請選擇贈送表情包的好友'
  } else if (locale === 'ja') {
    successMsgTip.operation = '操作が成功しました'
    successMsgTip.RGisInside = '内部メンバーのリチャージに成功'
    successMsgTip.upgrade = '内部メンバーのアップグレードに成功しました！'
    successMsgTip.translate = '翻訳を購入する'
    successMsgTip.RG = '正常に充電してください'
    successMsgTip.islike = 'あなたはすでにこの友達が好きです'
    successMsgTip.setuser = 'ユーザー情報が正常に変更されました！'
    successMsgTip.noData = 'これ以上のデータはありません!'
    successMsgTip.friendMsg = '新しいニュースを確認してください'
    successMsgTip.giftMsg = '友達から贈り物を受け取りました'
    successMsgTip.msgEmpty = '動的メッセージは空にできません'
    successMsgTip.codeError = '確認コードを入力してください'
    successMsgTip.newPwdError = '新しいパスワードを入力してください'
    successMsgTip.buyerr = '表情包すでに经购买过了！'
    successMsgTip.buysucess = '絵文字の購入に成功しました！'
    successMsgTip.buyNoCheck = '購入する絵文字を選択してください！'
    successMsgTip.buyNoCheckF = '絵文字を付ける友達を選択してください'
  }
  return successMsgTip[name]
}
export default utils
